<?php
namespace Admin\Models\res;

use Phalcon\Di;
use Helpers\CurlClient as HttpClient;
use Admin\Models\InterfaceService;

/**
 * Class Books 书本
 * @package Admin\Models\Question
 */
class Books extends InterfaceService
{
    private static $interfaceKey = 'questionBaseUrl';
    private static $otherInterfaceKey = 'questionPrivilegeUrl';

    /**
     * #方法说明：题库数据层
     * # Created by wy
     */
    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 方法说明：创建httpUrl
     *
     * @param $httpUrl
     * @param array $paramArr
     *
     * @return bool|string
     *
     * create by wy
     */
    public static function createHttpUrl($httpUrl, $paramArr = array())
    {
        return parent::createInterfaceUrl($httpUrl, $paramArr, self::$interfaceKey);
    }

    /**
     * #方法说明：获取所有校本库信息
     * @param $paramArr
     * @return bool|mixed|string
    # Created by wy
     * modify by qgz 20180525 使用同一新的接口
     */
    public function getAllBookList($paramArr)
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl('/api/v2/books/adminCoach'));
        $result = $http->get(null, $paramArr)->headerConvert();
        return $http->bodyConvert();
    }

    /**
     * 修改书本
     *
     * @param $bookId
     * @param $paramArr
     *
     * @return bool|mixed|string
     *
     * create by qgz 20181024
     */
    public function modifyBook($bookId, $paramArr)
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl('/api/v1/books/' . $bookId));
        $result = $http->patchBody(null, $paramArr)->headerConvert();
        return $http->bodyConvert();
    }

    /**
     * 获取所有枚举类型
     *
     * @return bool|mixed|string
     *
     * create by qgz 2018/5/9
     */
    public function getEnumTypeList()
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl('/api/v1/dictionaries'));
        $result = $http->get(null, [])->headerConvert();
        return $http->bodyConvert();
    }

    /**
     * 获取全部数据
     *
     * @return bool|mixed|string
     *
     * create by qgz 20180525
     */
    public function getBookData()
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl('/api/v1/books/textBookVersionCompare'));
        $result = $http->get(null, [])->headerConvert();
        return $http->bodyConvert();
    }

    /**
     * 资源库查询书本--新接口
     *
     * @param $paramArr
     *
     * @return bool|mixed|string
     *
     * create by qgz 20180911
     */
    public function getNewAllBookList($paramArr)
    {
        $http = new HttpClient();
        $http->setBaseUrl(parent::createInterfaceUrl('/api/v1/books/schoolCount', [], self::$otherInterfaceKey));
        $result = $http->get(null, $paramArr)->headerConvert();
        return $http->bodyConvert();
    }
    
    /**
     * 根据书id获取书详情
     *
     * @param $bookId
     *
     * @return bool|mixed|string
     *
     * create by qgz 20180920
     */
    public function getBookDetail($bookId)
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl('/api/v1/books/' . $bookId));
        $result = $http->get(null, [])->headerConvert();
        return $http->bodyConvert();
    }

    /**
     * 根据书id查询该书的章节数据
     *
     * @param $bookId
     * @param $params
     *
     * @return bool|mixed|string
     *
     * create by qgz 20180927
     */
    public function getCatalogsByBookId($bookId, $params)
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl('/api/v1/books/' . trim($bookId) . '/catalogs/coach'));
        $result = $http->get(null, $params)->headerConvert();
        return $http->bodyConvert();
    }

    /**
     * 根据章节id获取章节数据（包括自己和父级的数据）
     *
     * @param $params
     *
     * @return bool|mixed|string
     *
     * create by qgz 20190220
     */
    public function getCatalogsByCatalogId($params)
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl('/api/v1/catalogs/treeCatalogList'));
        $result = $http->get(null, $params)->headerConvert();
        return $http->bodyConvert();
    }

    /**
     * 获取出版社信息
     *
     * @return bool|mixed|string
     *
     * create by qgz 20190226
     */
    public function getBookType()
    {
        $http = new HttpClient();
        $http->setBaseUrl(self::createHttpUrl('/api/v1/bookTypePage'));
        $result = $http->get(null, ['page' => 1, 'size' => 99999])->headerConvert();
        return $http->bodyConvert();
    }

//    /**
//     * 根据书id批量获取书信息
//     *
//     * @param $bookIdArr
//     *
//     * @return bool|mixed|string
//     *
//     * create by qgz 20181120
//     */
//    public function getBookDetailByArr($bookIdArr)
//    {
//        $config = Main::getConfigByName('resServerInterface');//接口资源中心
//        $url = Tool::getValue($config, 'questionBaseUrl'); //资源权限接口
//        $http = new newHttpClient($url);
//        $response = $http->postJson('/api/v1/books/queryBooks', $bookIdArr);
//        $body = $response->toArray();
//        if (isset($body['code']) && $body['code'] != 200 && $body['code'] != 201 && $body['code'] != 400) {
//            return [];
//        }
//        return $body;
//    }
}
